#!/bin/sh

PREPARE=$1
LANG="$2"
CVERSION=4.6rc3
VERSION="$(cat data/eversion)"
CFG="eCore.cfg"
ISODIR="/mnt/sda2/eCore"
TESTDIR="/mnt/sda1/boot/iso"
THISDIR=`pwd`
CDLOCATION=/mnt/sda2/distro/Core-$CVERSION.iso
EZTMPDIR=/mnt/sda2/tmp/ezremaster/

[ "$LANG" ] && LANG=_$LANG
if [ ! -f /usr/local/tce.installed/mkisofs-tools ]; then
   if [ ! -e /etc/sysconfig/tcedir/optional/mkisofs-tools.tcz ]; then
      tce-load -w mkisofs-tools
   fi
      tce-load -is mkisofs-tools
fi
if [ ! -f /usr/local/tce.installed/advcomp ]; then
   if [ ! -e /etc/sysconfig/tcedir/optional/advcomp.tcz ]; then
      tce-load -w advcomp
   fi
      tce-load -is advcomp
fi
if [ ! "$PREPARE" = "-np" ];then
	[ -f allextensions.lst ] && sudo rm 	   allextensions.lst    
	cp  data/instlist/standard.instlist    	   allextensions.lst    
	cat data/instlist/ndiswrapper.instlist  >> allextensions.lst
	cat data/instlist/wififirmware.instlist >> allextensions.lst
	cat data/instlist/wifi.instlist 	>> allextensions.lst
	echo Xvesa.tcz		  		>> allextensions.lst
	echo Xorg-7.6-3d.tcz	  		>> allextensions.lst
	echo xf86-video-all.tcz	  		>> allextensions.lst
	echo Mplayer-xorg.tcz	  		>> allextensions.lst
	echo Mplayer-nodeps.tcz	  		>> allextensions.lst
	echo "cd_location = $CDLOCATION" 	   	  	  >  $CFG
	echo "temp_dir = $EZTMPDIR"		  	  	  >> $CFG
	echo "app_extract_initrd = exquisite.tcz" 	  	  >> $CFG
	echo "app_extract_initrd = Xlibs.tcz" 	  	  	  >> $CFG
	echo "extract_tcz_script = chroot" 	  	  	  >> $CFG
	sed "s/^/app_outside_initrd_onboot = /" allextensions.lst >> $CFG
#	echo -n "ezremaster cfg file created.Press Enter key."
#	read gagme
	sudo rm -rf ezremaster/*
	./remaster.sh $CFG checkimage
	./remaster.sh $CFG extractimage
	sudo mv ezremaster/image/boot/core.gz ezremaster/image/boot/coreorig.gz
	#./remaster.sh $CFG bootcode
	sleep 4
#	echo -n "Next apps.Press Enter key."
#	read gagme
	./remaster.sh $CFG apps
fi
sudo rm onboot/*
cp  data/instlist/standard.instlist 	   onboot/onboot.lst
echo Mplayer-xorg.tcz       		>> onboot/onboot.lst
echo Xorg-7.6-3d.tcz 	    		>> onboot/onboot.lst
echo xf86-video-all.tcz     		>> onboot/onboot.lst
cp  data/instlist/standard.instlist 	   onboot/onbootno3dwifi.lst
cat data/instlist/ndiswrapper.instlist  >> onboot/onbootno3dwifi.lst
cat data/instlist/wififirmware.instlist >> onboot/onbootno3dwifi.lst
cat data/instlist/wifi.instlist 	>> onboot/onbootno3dwifi.lst
echo Mplayer-xorg.tcz       		>> onboot/onbootno3dwifi.lst
echo Xorg-7.6.tcz 	    		>> onboot/onbootno3dwifi.lst
cp  data/instlist/standard.instlist 	   onboot/onbootwifi.lst
cat data/instlist/ndiswrapper.instlist  >> onboot/onbootwifi.lst
cat data/instlist/wififirmware.instlist >> onboot/onbootwifi.lst
cat data/instlist/wifi.instlist 	>> onboot/onbootwifi.lst
echo Mplayer-xorg.tcz       		>> onboot/onbootwifi.lst
echo Xorg-7.6-3d.tcz 	    		>> onboot/onbootwifi.lst
echo xf86-video-all.tcz     		>> onboot/onbootwifi.lst
cp  data/instlist/standard.instlist 	   onboot/onbootxvesawifi.lst
cat data/instlist/ndiswrapper.instlist  >> onboot/onbootxvesawifi.lst
cat data/instlist/wififirmware.instlist >> onboot/onbootxvesawifi.lst
cat data/instlist/wifi.instlist 	>> onboot/onbootxvesawifi.lst
echo Mplayer-nodeps.tcz	    		>> onboot/onbootxvesawifi.lst
echo Xvesa.tcz	 	    		>> onboot/onbootxvesawifi.lst
cp  data/instlist/standard.instlist 	   onboot/onbootno3d.lst
echo Mplayer-xorg.tcz       		>> onboot/onbootno3d.lst
echo Xorg-7.6.tcz 	    		>> onboot/onbootno3d.lst
cp  data/instlist/standard.instlist 	   onboot/onbootxvesa.lst
echo Mplayer-nodeps.tcz	    		>> onboot/onbootxvesa.lst
echo Xvesa.tcz	 	    		>> onboot/onbootxvesa.lst

#echo -n "onboot files created.Press Enter key."
#read gagme
sudo mv ezremaster/image/cde ezremaster/image/tce
cd ezremaster/image/
ln -s tce cde
cd "$THISDIR"
if [ "$LANG" = "_it" ];then 
   sudo cp data/iso/boot_it.msg 	ezremaster/image/boot/isolinux/boot.msg
else
   sudo cp data/iso/isolinux.cfg 	ezremaster/image/boot/isolinux
   sudo cp data/iso/boot.msg 		ezremaster/image/boot/isolinux
   sudo cp data/iso/boot.msg 		ezremaster/image/boot/isolinux/f1
fi
sudo cp data/iso/menu.c32		ezremaster/image/boot/isolinux/
sudo cp data/iso/isolinux.bin		ezremaster/image/boot/isolinux/
sudo cp onboot/*.lst 			ezremaster/image/tce
sudo cp data/copy2fs.lst 		ezremaster/image/tce
sudo cp data/scripts/tc-config   	ezremaster/extract/etc/init.d/
sudo cp data/scripts/e-tc-functions   	ezremaster/extract/etc/init.d/
sudo cp data/scripts/tce-setup   	ezremaster/extract/usr/bin/
sudo cp data/scripts/fromISOfile 	ezremaster/extract/usr/bin/
sudo cp data/scripts/filetool.sh 	ezremaster/extract/usr/bin/
sudo cp remaster.sh		 	ezremaster/extract/usr/local/bin/
sudo cp data/eversion    		ezremaster/extract/usr/share/doc/tc/erelease.txt
sudo cp data/eCore.edj    		ezremaster/extract/usr/local/share/exquisite/data/themes/
sudo cp data/scripts/bootlocal.sh	ezremaster/extract/opt/
sudo mkdir -p				ezremaster/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
sudo cp data/mplayer.desktop		ezremaster/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
#sudo cp data/.xsession    		ezremaster/extract/etc/skel/
#sudo cp data/entrance.conf    		ezremaster/extract/usr/local/etc/entrance.conf
sleep 4
#echo -n "Next step package. Press Enter key."
#read gagme
./remaster.sh $CFG package
cp ezremaster/ezremaster.iso "$TESTDIR"
sudo mv ezremaster/ezremaster.iso "$ISODIR/eCore-$VERSION.iso"
